#!/usr/bin/env python
import sys
"""
    personal self-defined color print function.
"""

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)

class ColorPrint:

    #following from Python cookbook, #475186
    def has_colours(stream):
        if not hasattr(stream, "isatty"):
            return False
        if not stream.isatty():
            return False # auto color only on TTYs
        try:
            import curses
            curses.setupterm()
            return curses.tigetnum("colors") > 2
        except:
            # guess false in case of error
            return False
    # whether current system support color display.
    has_colours = has_colours(sys.stdout)

    def print_color(text, colour=WHITE):
        if has_colours:
                seq = "\x1b[1;%dm" % (30+colour) + text + "\x1b[0m"
                sys.stdout.write(seq)
        else:
                sys.stdout.write(text)

if __name__ == '__main__':
    
    hello="joseph"
    color = iColor()
    color.print_color("[Debug]", GREEN)
    print("in green")
    color.print_color("[warning] ", YELLOW)
    print("in yellow")
    color.print_color("[error]   ", RED)
    print("in red")
